package com.breezelin.freshmarket.entities.data;

/**
 * Created by Breeze Lin
 * 10:24 2016/5/15
 * 1linyufeng1@gmail.com
 */

import com.breezelin.fmlib.utils.TextUtils;
import com.breezelin.freshmarket.entities.JsonStruct;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * 产品详情
 */
public class ProductDetail implements JsonStruct {

	/**
	 * 产品标识——无
	 */
	public static final String TAG_NONE = "0";
	/**
	 * 产品标识——新品
	 */
	public static final String TAG_NEW = "1";

	/**
	 * 货物id
	 */
	private String commodityId;
	/**
	 * 货物代码
	 */
	private String commodityCode;
	/**
	 * 货物名称
	 */
	private String commodityName;
	/**
	 * 原价
	 */
	private String originalPrice;
	/**
	 * 价格
	 */
	private String commodityPrice;
	/**
	 * 状态
	 */
	private String state;
	/**
	 * 状态文本
	 */
	private String stateText;
	/**
	 * 规格
	 */
	private String spec;
	/**
	 * todo 未知字段
	 */
	private String brandName;
	/**
	 * 产地
	 */
	private String placeOfOrigin;
	/**
	 * 运送目的地
	 */
	private String deliveryAreas;
	/**
	 * todo 未知字段
	 */
	private String defaultAreaText;
	/**
	 * 产品到期时间文本
	 */
	private String closeTimeText;
	/**
	 * 送货日
	 */
	private String deliveryDateText;
	/**
	 * 可否无理由退货
	 */
	private String canNoReasonToReturn;
	/**
	 * 好评率
	 */
	private String votePositiveRate;
	/**
	 * 评价人数
	 */
	private String voteCount;
	/**
	 * 副标题
	 */
	private String subTitle;
	/**
	 * 产品tag
	 */
	private String commodityTag;
	/**
	 * tag图片
	 */
	private String commodityTagPicture;
	/**
	 * 描述网址（广告页）
	 */
	private String descriptionUrl;
	/**
	 * 是否显示原价
	 */
	private String showOriginalPrice;
	/**
	 * 规格
	 */
	private LinkedList<Spec> speces;
	/**
	 * 产品展示图片
	 */
	private LinkedList<String> pictures;
	/**
	 * 折扣或活动
	 */
	private LinkedList<Discount> promotions;
	/**
	 * 是否移动端独占价格
	 */
	private String isAppOnlyPrice;
	/**
	 * 限购数
	 */
	private String maxLimitCount;
	/**
	 * 送货提示
	 */
	private String deliveryTips;
	/**
	 * 分享标题
	 */
	private String shareTitle;
	/**
	 * 分享文案
	 */
	private String shareContent;
	/**
	 * 分享图片
	 */
	private String sharePicture;
	/**
	 * 分享链接地址
	 */
	private String shareUrl;

	public ProductDetail(JSONObject jsonObject) {
		fromJson(jsonObject);
	}

	public ProductDetail() {
	}

	public String getCommodityId() {
		return commodityId;
	}

	public String getCommodityCode() {
		return commodityCode;
	}

	public String getCommodityName() {
		return commodityName;
	}

	public String getOriginalPrice() {
		return originalPrice;
	}

	public String getCommodityPrice() {
		return commodityPrice;
	}

	public String getState() {
		return state;
	}

	public String getStateText() {
		return stateText;
	}

	public String getSpec() {
		return spec;
	}

	public String getBrandName() {
		return brandName;
	}

	public String getPlaceOfOrigin() {
		return placeOfOrigin;
	}

	public String getDeliveryAreas() {
		return deliveryAreas;
	}

	public String getDefaultAreaText() {
		return defaultAreaText;
	}

	public String getCloseTimeText() {
		return closeTimeText;
	}

	public String getDeliveryDateText() {
		return deliveryDateText;
	}

	public boolean canNoReasonToReturn() {
		return canNoReasonToReturn.equals("1");
	}

	public String getVotePositiveRate() {
		return votePositiveRate;
	}

	public int getVoteCount() {
		if (!TextUtils.isEmpty(voteCount)) {
			return Integer.valueOf(voteCount);
		} else {
			return 0;
		}
	}

	public String getSubTitle() {
		return subTitle;
	}

	public String getCommodityTag() {
		return commodityTag;
	}

	public String getCommodityTagPicture() {
		return commodityTagPicture;
	}

	public String getDescriptionUrl() {
		return descriptionUrl;
	}

	public boolean getShowOriginalPrice() {
		return showOriginalPrice.equals("1");
	}

	public LinkedList<Spec> getSpeces() {
		return speces;
	}

	public LinkedList<String> getPictures() {
		return pictures;
	}

	public LinkedList<Discount> getPromotions() {
		return promotions;
	}

	public boolean isAppOnlyPrice() {
		return isAppOnlyPrice.equals("1");
	}

	public int getMaxLimitCount() {
		if (!TextUtils.isEmpty(maxLimitCount)) {
			return Integer.valueOf(maxLimitCount);
		} else {
			return 0;
		}
	}

	public String getDeliveryTips() {
		return deliveryTips;
	}

	public String getShareTitle() {
		return shareTitle;
	}

	public String getShareContent() {
		return shareContent;
	}

	public String getSharePicture() {
		return sharePicture;
	}

	public String getShareUrl() {
		return shareUrl;
	}

	@Override
	public void fromJson(JSONObject jsonObject) {
		commodityId = jsonObject.optString("CommodityId");
		commodityCode = jsonObject.optString("CommodityCode");
		commodityName = jsonObject.optString("CommodityName");
		originalPrice = jsonObject.optString("OriginalPrice");
		commodityPrice = jsonObject.optString("CommodityPrice");
		state = jsonObject.optString("State");
		stateText = jsonObject.optString("StateText");
		spec = jsonObject.optString("Spec");
		brandName = jsonObject.optString("BrandName");
		placeOfOrigin = jsonObject.optString("PlaceOfOrigin");
		deliveryAreas = jsonObject.optString("DeliveryAreas");
		defaultAreaText = jsonObject.optString("DefaultAreaText");
		closeTimeText = jsonObject.optString("CloseTimeText");
		deliveryDateText = jsonObject.optString("DeliveryDateText");
		canNoReasonToReturn = jsonObject.optString("CanNoReasonToReturn");
		votePositiveRate = jsonObject.optString("VotePositiveRate");
		voteCount = jsonObject.optString("VoteCount");
		subTitle = jsonObject.optString("Subtitle");
		commodityTag = jsonObject.optString("CommodityTag");
		commodityTagPicture = jsonObject.optString("CommodityTagPicture");
		descriptionUrl = jsonObject.optString("DescriptionUrl");
		showOriginalPrice = jsonObject.optString("ShowOriginalPrice");
		JSONArray specesJson = jsonObject.optJSONArray("Speces");
		speces = new LinkedList<>();
		{
			int length = 0;
			if (specesJson != null) {
				length = specesJson.length();
			}
			for (int i = 0; i < length; i++) {
				speces.add(new Spec(specesJson.optJSONObject(i)));
			}
		}
		JSONArray picturesJson = jsonObject.optJSONArray("Pictures");
		pictures = new LinkedList<>();
		{
			int length = 0;
			if (picturesJson != null) {
				length = picturesJson.length();
			}
			for (int i = 0; i < length; i++) {
				pictures.add(picturesJson.optString(i));
			}
		}
		promotions = new LinkedList<>();
		JSONArray promotionsJson = jsonObject.optJSONArray("Promotions");
		{
			int length = 0;
			if (promotionsJson != null) {
				length = promotionsJson.length();
			}
			for (int i = 0; i < length; i++) {
				promotions.add(new Discount(promotionsJson.optJSONObject(i)));
			}
		}
		isAppOnlyPrice = jsonObject.optString("IsAppOnlyPrice");
		maxLimitCount = jsonObject.optString("MaxLimitCount");
		deliveryTips = jsonObject.optString("DeliveryTips");
		shareTitle = jsonObject.optString("ShareTitle");
		shareContent = jsonObject.optString("ShareContent");
		sharePicture = jsonObject.optString("SharePicture");
		shareUrl = jsonObject.optString("ShareUrl");
	}

	@Override
	public JSONObject toJson() {
		return new JSONObject();
	}
}